package com.battle.service.listener;

import com.battle.pojo.event.department.CreateDepartmentEvent;
import com.battle.pojo.event.department.DeleteDepartmentEvent;
import com.battle.pojo.event.resource.CreateResourceEvent;
import com.battle.pojo.event.tenant.DeleteTenantEvent;
import com.battle.service.service.in.TreeNodeService;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

/**
 * @author Joseph.Li
 * @author joseph.li@king-v.com
 * Created in 2022-07-05 1:32
 */
@Service
public class TreeNodeListener {


    private final TreeNodeService treeNodeService;

    public TreeNodeListener(TreeNodeService treeNodeService) {
        this.treeNodeService = treeNodeService;
    }

    /**
     * 监听创建部门事件
     * @param event
     */
    @EventListener(CreateDepartmentEvent.class)
    public void treeNodeListenerCreateDepartmentEvent(CreateDepartmentEvent event){
        Long parentId = event.getParentId();
        Long id = event.getId();
        treeNodeService.addTreeNode(parentId,id);
    }


    /**
     * 创建资源事件
     * @param event
     */
    @EventListener(CreateResourceEvent.class)
    public void treeNodeListenerCreateResourceEvent(CreateResourceEvent event){
        Long parentId = event.getParentId();
        Long id = event.getId();
        treeNodeService.addTreeNode(parentId,id);

    }

    /**
     * 监听删除部门事件
     *
     * 删除所有下级部门干练数据
     * @param event
     */
    @EventListener(DeleteDepartmentEvent.class)
    public void treeNodeListener(DeleteDepartmentEvent event){
        Long departmentId = event.getDepartmentId();
        treeNodeService.deleteDescendant(departmentId);
    }


    /**
     * 删除企业事件
     *
     * 删除所有关联树
     * @param event
     */
    @EventListener(DeleteTenantEvent.class)
    public void treeNodeListener(DeleteTenantEvent event){
        Long topDepartmentId = event.getTopDepartmentId();
        treeNodeService.deleteDescendant(topDepartmentId);
    }


}
